//
//  ViewController.m
//  Charles
//
//  Created by Charles on 2017/2/7.
//  Copyright © 2017年 charles. All rights reserved.
//

#import "CLMainViewController.h"
#import "CLMasonryViewController.h"
#import "CLRuntimeViewController.h"
#import "CLCycleScollViewViewController.h"

#import "CLIMExpressionViewController.h"
#import "CLAdsorbButtonViewController.h"
#import "CLCollectionViewController.h"
#import "CLThreadViewController.h"
#import "CLMemoryViewController.h"
#import "CLMvvmViewController.h"
#import "CLCacheCellViewController.h"
#import "CLCycleCollectionViewController.h"
#import "RoomListViewController.h"
#import "CLReuseViewController.h"
#import "DecoratorTest.h"
#import "StrategyTest.h"
#import "ObserverTest.h"
#import "FactoryTest.h"
#import "SingletonTest.h"
#import "ICTest.h"
#import "CLNavigationController.h"

static const NSString *kPages = @"Pages";
static const NSString *kPageViewName = @"PageViewName";
static const NSString *kPageTitle = @"PageTitle";

@interface CLMainViewController () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView * tableView;
@property (strong, nonatomic) NSArray *pageArray;

@end

@implementation CLMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Charles";
    
    [self setUpData];
    [self setUpViews];
}
- (void)setUpData {
  //@[@"Masnory",@"Runtime",@"CycleScrollView",@"IM&AttributedLabel",@"AdsorbButton",@"CollectionView",@"Thread",@"Memory",@"MVVM",@"缓存Cell高度",@"装饰者模式",@"策略模式",@"观察者模式",@"工厂模式",@"单例模式",@"动画对队列",@"循环滚动的CollectionView",@"AirBnb过渡动画",@"tableView重用"];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"CLPages" ofType:@"plist"];
    NSDictionary *pageDict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSArray *pages = [pageDict objectForKey:kPages];
    NSAssert(pages, @"页面为空");
    self.pageArray = [NSArray arrayWithArray:pages];
}

- (void)setUpViews {
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(0);
    }];
}

- (UITableView *)tableView {
    return CL_LAZY(_tableView,({
        UITableView *table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        table.dataSource = self;
        table.delegate = self;
        [self.view addSubview:table];
        table;
    }));
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45.0f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.pageArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary * pageDict = self.pageArray[indexPath.row];
    
    NSString *className = [pageDict objectForKey:kPageViewName];
    NSString *pageTitle = [pageDict objectForKey:kPageTitle];
    NSParameterAssert(className);
    Class pageClass = NSClassFromString(className);
    UIViewController *pushView = [[pageClass alloc]init];
    pushView.title = pageTitle;
    [self.navigationController pushViewController:pushView animated:YES];
    
    /*
    if (0 == indexPath.row) {
        CLMasonryViewController *containView = [[CLMasonryViewController alloc]init];
        containView.title = title;
        [self pushViewController:containView];
    }else if (1== indexPath.row) {
        CLRuntimeViewController *runtimeView = [[CLRuntimeViewController alloc]init];
        runtimeView.title = title;
        [self pushViewController:runtimeView];
    }else if (2 == indexPath.row) {
        CLCycleScollViewViewController *cycleScrollView = [[CLCycleScollViewViewController alloc]init];
        cycleScrollView.title = title;
        [self pushViewController:cycleScrollView];
    }else if (3 == indexPath.row) {
        CLIMExpressionViewController *expressionView = [[CLIMExpressionViewController alloc]init];
        expressionView.title = title;
        [self pushViewController:expressionView];
    }else if (4 == indexPath.row) {
        CLAdsorbButtonViewController *view = [[CLAdsorbButtonViewController alloc]init];
        [self pushViewController:view];
    }else if (5 == indexPath.row) {
        CLCollectionViewController *collectionView = [[CLCollectionViewController alloc]init];
        [self pushViewController:collectionView];
    }else if (6 == indexPath.row) {
        CLThreadViewController *threadView = [[CLThreadViewController alloc]init];
        [self pushViewController:threadView];
    }else if (7 == indexPath.row) {
        CLMemoryViewController * memoryView = [[CLMemoryViewController alloc]init];
        [self pushViewController:memoryView];
    }else if (8 == indexPath.row) {
        CLMvvmViewController *mvvmView = [[CLMvvmViewController alloc]init];
        [self pushViewController:mvvmView];
    }else if (9 == indexPath.row) {
        CLCacheCellViewController *cacheView = [[CLCacheCellViewController alloc]init];
        [self pushViewController:cacheView];
    }
    else if (10 == indexPath.row) {
        [self testDecorator];
    }else if (11 == indexPath.row) {
        [self testStrategy];
    }else if (12 == indexPath.row) {
        [self testObserver];
    }else if (13 == indexPath.row) {
        [self testFactory];
    }else if (14 == indexPath.row) {
        [self testSingleton];
    }else if (15 == indexPath.row) {
        [self testIco];

    }else if (16 == indexPath.row) {
        CLCycleCollectionViewController *collectionView = [[CLCycleCollectionViewController alloc]init];
        [self pushViewController:collectionView];
    }else if (17 == indexPath.row) {
                
        RoomListViewController *roomList = [[RoomListViewController alloc]init];
        UINavigationController *navCtrl = [[UINavigationController alloc]initWithRootViewController:roomList];
        navCtrl.view.backgroundColor = [UIColor whiteColor];
        [self.navigationController presentViewController:navCtrl animated:YES completion:nil];
        
    }else if (18 == indexPath.row) {
        CLReuseViewController *reuseViewController = [[CLReuseViewController alloc]init];
        [self pushViewController:reuseViewController];
    }
     */
}

- (void)pushViewController:(UIViewController *)viewController {
    [self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - UITableVeiwDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * strId = @"Id";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:strId];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strId];
    }
    if (indexPath.row < self.pageArray.count) {
        NSDictionary *pageDict = [self.pageArray objectAtIndex:indexPath.row];
        NSString * title = [pageDict objectForKey:kPageTitle];
        cell.textLabel.text = title;
    }
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end



